// C11:ResponsiveUI.cpp {RunByHand}
//  Threading for a responsive user interface.
//  {L} ZThread
#include "zthread/Thread.h"
#include <fstream>
#include <iostream>
#include <string>

using namespace ZThread;
using namespace std;

class DisplayTask : public Runnable {
  ifstream in;
  string line;
  bool quitFlag;

public:
  DisplayTask(const string &file) : quitFlag(false) { in.open(file.c_str()); }
  ~DisplayTask() { in.close(); }
  void run() {
    while (getline(in, line) && !quitFlag) {
      cout << line << endl;
      Thread::sleep(1000);
    }
  }

  void quit() { quitFlag = true; }
};

int main(void) {
  try {
    cout << "Press <Enter> to quit:" << endl;
    DisplayTask *dt = new DisplayTask("ResponsiveUI.cpp");
    Thread t(dt);
    cin.get();
    dt->quit();
  } catch (Synchronization_Exception &e) {
    cerr << e.what() << endl;
  }

  cout << "Shutting down..." << endl;
}
